#!/bin/bash

# http://dadaru.blog.51cto.com/218979/477236

# Check if user is root
if [ $(id -u) != "0" ]; then
    echo "Error: You must be root to run this script, please use root to install lnmp"
    exit 1
fi

#downurl
#nginx			http://nginx.org/download/nginx-1.0.1.tar.gz
#php			http://cn.php.net/get/php-5.3.6.tar.gz/from/this/mirror
#mysql			http://dev.mysql.com/get/Downloads/MySQL-5.5/mysql-5.5.12.tar.gz/from/http://mirror.services.wisc.edu/mysql/
#memcached		http://memcached.googlecode.com/files/memcached-1.4.5.tar.gz
#memcache		http://pecl.php.net/get/memcache-2.2.5.tgz
#eaccelerator	http://bart.eaccelerator.net/source/0.9.6.1/eaccelerator-0.9.6.1.tar.bz2
#phpMyAdmin	http://downloads.sourceforge.net/project/phpmyadmin/phpMyAdmin/3.3.10/phpMyAdmin-3.3.10-all-languages.tar.gz?r=http%3A%2F%2Fwww.phpmyadmin.net%2Fhome_page%2Fdownloads.php&ts=1304930983&use_mirror=nchc


#---------------------config start-------------------------

nginx_dir_name="nginx-1.0.1"
mysql_dir_name="mysql-5.5.12"
php_dir_name="php-5.3.6"
memcached_dir_name="memcache-1.4.5"
eaccelerator_dir_name="eaccelerator-0.9.6.1"
phpMyAdmin_dir_name="phpMyAdmin-3.2.5-all-languages"

libiconv_dir_name="libiconv-1.13.1"
libevent_dir_name="libevent-1.4.13-stable"

#-

nginx_suffix=".tar.gz"
mysql_suffix=".tar.gz"
php_suffix=".tar.gz"
memcached_suffix=".tgr.gz"
eaccelerator_suffix=".tar.bz2"
phpMyAdmin_suffix=".tar.gz"

libiconv_suffix=".tar.gz"
libevent_suffix=".tar.gz"

#-

install_path="/home/webserv"
mysql_password="985631"
curr_dir=$(pwd)
#---------------------config end---------------------------

clear
echo "========================================================================="
echo "AutoXNPM v1.2 for Ubuntu by kth007 "
echo "========================================================================="
echo "A tool to auto-compile & install nginx+mysql+php on Ubuntu "
echo "For more information please visit http://code.google.com/p/unmp/"
echo ""
echo "The path of some dirs (If install to /usr/local/):"
echo "php dir:     $install_path/php"
echo "nginx dir:   $install_path/nginx"
echo "mysql dir:   $install_path/mysql"
echo "web dir:     /home/var/www"
echo "mysql data:  /home/var/mysql"
echo ""
echo "Software version:"
echo "nginx:         $nginx_dir_name"
echo "mysql:         $mysql_dir_name"
echo "php:           $php_dir_name"
echo "php_fpm:       $php_fpm_dir_name"
echo "memcache:      $memcache_dir_name"
echo "eaccelerator:  $eaccelerator_dir_name"
echo "phpMyAdmin:    $phpMyAdmin_dir_name"
echo "========================================================================="

get_char()
{
	SAVEDSTTY=`stty -g`
	stty -echo
	stty cbreak
	dd if=/dev/tty bs=1 count=1 2> /dev/null
	stty -raw
	stty echo
	stty $SAVEDSTTY
}
echo ""
echo "Press any key to start..."
char=`get_char`

rm -rf $nginx_dir_name
rm -rf $mysql_dir_name
rm -rf $libiconv_dir_name
rm -rf $autoconf_dir_name
rm -rf $libevent_dir_name
rm -rf $php_dir_name
rm -rf $memcached_dir_name
rm -rf $eaccelerator_dir_name
rm -rf $PDO_MYSQL_dir_name
rm -rf $phpMyAdmin_dir_name


clear
echo "==Download Install Pack=="

apt-get -y install libxml2-dev libmcrypt-dev libssl-dev libldap2-dev libmhash-dev libmysqlclient-dev libcurl4-openssl-dev libpng12-dev libjpeg62-dev libsasl2-dev libdb4.7-dev libfreetype6-dev gcc gcc-c++ g++ build-essential libpcre3-dev libssl-dev zlib1g-dev openssl autoconf libncurses5-dev 

clear
echo "==MAKE Install NGINX=="
groupadd www
useradd -g www www

mkdir -p $install_path
mkdir -p /home/var/www
chmod +w /home/var/www
chown -R www:www /home/var/www

mkdir -p /home/var/log/nginx
chmod +w /home/var/log/nginx
chown -R www:www /home/var/log/nginx
touch /home/var/log/nginx/error.log

cd $curr_dir
tar zxvf $nginx_dir_name$nginx_suffix
cd $nginx_dir_name/
./configure  --prefix=$install_path/nginx/ --user=www --group=www --with-http_stub_status_module --with-http_ssl_module --with-http_gzip_static_module
make
make install

mkdir -p $install_path/init

rm -f $install_path/nginx/conf/nginx.conf
cp $curr_dir/conf/nginx.conf $install_path/nginx/conf/nginx.conf
rm -f $install_path/nginx/conf/fcgi.conf
cp $curr_dir/conf/fcgi.conf $install_path/nginx/conf/fcgi.conf
cp $curr_dir/conf/nginx $install_path/nginx/nginx
ln -s $install_path/nginx/nginx $install_path/init/nginx

mkdir -p $install_path/nginx/conf/sites-available
mkdir -p $install_path/nginx/conf/sites-enabled
cp $curr_dir/conf/default $install_path/nginx/conf/sites-available/default
ln -s $install_path/nginx/conf/sites-available/default $install_path/nginx/conf/sites-enabled/default
cd ../


clear
echo "==Install MYSQL=="

groupadd mysql
useradd -g mysql mysql
mkdir -p /var/mysql
chmod +w /var/mysql
chown -R mysql:mysql /var/mysql

cd $curr_dir
tar zxvf $mysql_dir_name$mysql_suffix
cd $mysql_dir_name/
./configure --prefix=$install_path/mysql/ --enable-assembler --with-extra-charsets=complex --enable-thread-safe-client --with-big-tables --with-readline --with-ssl --with-embedded-server --enable-local-infile --with-plugins=innobase
make
make install
cp $curr_dir/conf/my.cnf $install_path/mysql/my.cnf
ln -s $install_path/mysql/my.cnf /etc/my.cnf
cp $install_path/mysql/share/mysql/mysql.server $install_path/mysql/mysql
ln -s $install_path/mysql/mysql $install_path/init/mysql
$install_path/mysql/bin/mysql_install_db --basedir=$install_path/mysql --datadir=/var/mysql --user=mysql

$install_path/init/mysql start
$install_path/mysql/bin/mysql << EOF
use mysql;
UPDATE user SET password=PASSWORD("123456") WHERE user='root';
FLUSH PRIVILEGES;
exit
EOF
$install_path/init/mysql stop
cd ../


clear
echo "==MAKE Install PHP library=="

cd $curr_dir
tar zxvf $libiconv_dir_name$libiconv_suffix
cd $libiconv_dir_name/
./configure --prefix=/usr/local/libiconv
make
make install
cd ../

cd $curr_dir
tar zxvf $autoconf_dir_name$autoconf_dir_suffix
cd $autoconf_dir_name/
./configure --prefix=/usr/local/autoconf
make
make install
export PHP_AUTOCONF=/usr/local/autoconf/bin/autoconf
export PHP_AUTOHEADER=/usr/local/autoconf/bin/autoheader
cd ../

cd $curr_dir
tar zxvf $libevent_dir_name$libevent_suffix
cd $libevent_dir_name/
./configure --prefix=/usr/local/libevent
make
make install
cd ../


clear
echo "==MAKE Install PHP=="

cd $curr_dir
tar zxvf $php_dir_name$php_suffix
gzip -cd $php_fpm_dir_name$php_fpm_dir_suffix | patch -d $php_dir_name -p1
cd $php_dir_name/
./buildconf --force
./configure --prefix=$install_path/php --with-config-file-path=$install_path/php/etc --with-mysql --with-mysqli --with-iconv-dir=/usr/local/libiconv --with-freetype-dir --with-jpeg-dir --with-png-dir --with-zlib --with-libxml-dir=/usr --enable-xml --disable-rpath --enable-discard-path --enable-safe-mode --enable-bcmath --enable-shmop --enable-sysvsem --enable-inline-optimization --with-curl --with-curlwrappers --enable-mbregex --enable-fastcgi --enable-fpm --enable-force-cgi-redirect --enable-mbstring --with-mcrypt --with-gd --enable-gd-native-ttf --with-openssl --with-mhash --enable-pcntl --enable-sockets --with-xmlrpc --enable-zip --enable-soap --without-pear --with-mime-magic=/usr/share/file/magic.mime
make
make install

rm -f $install_path/php/etc/php-fpm.conf
cp $curr_dir/conf/php-fpm.conf $install_path/php/etc/php-fpm.conf
cp $curr_dir/conf/php.ini $install_path/php/etc/php.ini
ln -s $install_path/php/sbin/php-fpm $install_path/init/php-fpm
strip $install_path/php/bin/php-cgi
cd ../


clear
echo "==MAKE Install eaccelerator=="

mkdir $install_path/eaccelerator_cache
cd $curr_dir
tar jxvf $eaccelerator_dir_name$eaccelerator_suffix
cd $eaccelerator_dir_name/
$install_path/php/bin/phpize
./configure --enable-eaccelerator --with-php-config=$install_path/php/bin/php-config
make
make install
cd ../


clear
echo "==MAKE Install memcache=="
cd $curr_dir
tar zxvf $memcache_dir_name$memcache_dir_suffix
cd $memcache_dir_name/
$install_path/php/bin/phpize
./configure --with-php-config=$install_path/php/bin/php-config
make
make install
cd ../



clear
echo "==MAKE Install PDO_MYSQL=="
tar zxvf $PDO_MYSQL_dir_name$PDO_MYSQL_dir_suffix
cd $PDO_MYSQL_dir_name/
$install_path/php/bin/phpize
./configure --with-php-config=$install_path/php/bin/php-config --with-pdo-mysql=$install_path/mysql
make
make install
cd ../


clear
echo "==Install phpMyAdmin=="

cp $curr_dir/conf/index.php /var/www/index.php
cd $curr_dir
tar zxvf $phpMyAdmin_dir_name$phpMyAdmin_suffix
cp -r $phpMyAdmin_dir_name /var/www/phpMyAdmin


cat >/etc/sysctl.conf<<eof
# Add
net.ipv4.tcp_max_syn_backlog = 65536
net.core.netdev_max_backlog =  32768
net.core.somaxconn = 32768

net.core.wmem_default = 8388608
net.core.rmem_default = 8388608
net.core.rmem_max = 16777216
net.core.wmem_max = 16777216

net.ipv4.tcp_timestamps = 0
net.ipv4.tcp_synack_retries = 2
net.ipv4.tcp_syn_retries = 2

net.ipv4.tcp_tw_recycle = 1
#net.ipv4.tcp_tw_len = 1
net.ipv4.tcp_tw_reuse = 1

net.ipv4.tcp_mem = 94500000 915000000 927000000
net.ipv4.tcp_max_orphans = 3276800

#net.ipv4.tcp_fin_timeout = 30
#net.ipv4.tcp_keepalive_time = 120
net.ipv4.ip_local_port_range = 1024  65535

eof

/sbin/sysctl -p

cat >$install_path/init/webserv<<eof
#! /bin/sh

. /lib/lsb/init-functions

case "\$1" in
  start)
        echo -n "==Starting Webserv== \n"
        ulimit -SHn 51200
	$install_path/init/mysql start
        $install_path/init/php-fpm start
        $install_path/init/nginx start
        ;;
  stop)
        echo -n "==Stopping Webserv== \n"
        $install_path/init/nginx stop
        $install_path/init/php-fpm stop
        $install_path/init/mysql stop
        ;;
  restart)
        echo -n "==Restarting Webserv== \n"
        $install_path/init/php-fpm restart
        $install_path/init/nginx restart
        $install_path/init/mysql restart
        ;;
  *)
        echo "Usage: Webserv {start|stop|restart}" >&2
        exit 1
        ;;
esac

exit 0
eof

echo "* - nofile 51200" >>/etc/security/limits.conf

chmod 777 $install_path/init/webserv

ln -s $install_path/init/webserv /etc/init.d/webserv
update-rc.d webserv defaults

clear
echo "==start up=="

$install_path/init/mysql start
$install_path/init/php-fpm start
$install_path/init/nginx start

netstat -ntl

echo "==UNMP After installation=="
echo "URL:http://localhost/index.php"